package com.zycx.interceptor;

import com.zycx.common.constant.user.StringHelper;
import com.zycx.common.constant.user.UserConstants;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:  当前登陆用户
 * Created by zycx on 2018-12-05 23:41
 * Created with IntelliJ IDEA.
 */
public class CurrentUser {

    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key){
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    public static String getUserID(){
        Object value = get(UserConstants.CONTEXT_KEY_USER_ID);
        return returnObjectValue(value);
    }

    public static String getUserName(){
        Object value = get(UserConstants.CONTEXT_KEY_USERNAME);
        return returnObjectValue(value);
    }

    public static String getName(){
        Object value = get(UserConstants.CONTEXT_KEY_USER_NAME);
        return StringHelper.getObjectValue(value);
    }

    public static String getStatus(){
        Object value = get(UserConstants.CONTEXT_KEY_USER_STATUS);
        return StringHelper.getObjectValue(value);
    }

    public static String getSuperAdmin(){
        Object value = get(UserConstants.CONTEXT_KEY_USER_SUPER_ADMIN);
        return StringHelper.getObjectValue(value);
    }

    public static String getToken(){
        Object value = get(UserConstants.CONTEXT_KEY_USER_TOKEN);
        return StringHelper.getObjectValue(value);
    }
    public static void setToken(String token){set(UserConstants.CONTEXT_KEY_USER_TOKEN,token);}

    public static void setName(String name){set(UserConstants.CONTEXT_KEY_USER_NAME,name);}

    public static void setUserID(String userID){
        set(UserConstants.CONTEXT_KEY_USER_ID,userID);
    }

    public static void setUsername(String username){
        set(UserConstants.CONTEXT_KEY_USERNAME,username);
    }

    public static void setStatus(String status){set(UserConstants.CONTEXT_KEY_USER_STATUS,status);}

    public static void setSuperAdmin(String superAdmin){set(UserConstants.CONTEXT_KEY_USER_SUPER_ADMIN,superAdmin);}

    private static String returnObjectValue(Object value) {
        return value==null?null:value.toString();
    }

    public static void remove(){
        threadLocal.remove();
    }


}
